/**
 * Filename: UserStatusService.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/12 20:22
 */
package com.github.postsanf.yinian.service;

import android.content.Intent;
import android.util.Log;

import com.github.postsanf.yinian.BaseIntentService;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.YNStatus;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.constants.YNSingleton;
import com.github.postsanf.yinian.entity.YNGetUserResponse;
import com.github.postsanf.yinian.entity.YNLoginResponse;
import com.github.postsanf.yinian.entity.YNStatusResponse;
import com.github.postsanf.yinian.utils.ReqUtils;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 用户动态内容服务
 */
public class UserStatusService extends BaseIntentService {

    public UserStatusService() {
        super("UserStatusService");
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            final String action = intent.getAction();
            int userid = intent.getExtras().getInt(ACTIONs.aUserId, 0);
            boolean isme = intent.getExtras().getBoolean(ACTIONs.aISME);

            switch (action) {
                // 获取用户动态
                case ACTIONs.actionUserStatus:
                    int sid = intent.getExtras().getInt(ACTIONs.aLoadId);
                    int sgroupid = intent.getExtras().getInt(ACTIONs.aGroupId);
                    getUserStatus(userid, sid, sgroupid, isme);
                    break;
                case ACTIONs.actionGetTop:
                    Log.i("xjs,","actionGetTop");
                    getUserTop(userid);
                    getUserData(userid);
                    break;
            }
        }
    }

    // 获取成员头信息
    private void getUserTop(int userid) {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_USER_ID, String.valueOf(userid));
        StringRequest req = new StringRequest(getString(R.string.BASE_URL) + getString(R.string.ynShowUserHead))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        YNLoginResponse resUser = gson.fromJson(s, YNLoginResponse.class);
                        if (resUser != null && ReqUtils.isSuccess(resUser.getCode()) && resUser.getData().size() > 0) {
                            Intent upIntent = new Intent(ACTIONs.actionUserTop);
                            upIntent.putExtra(ACTIONs.aAddData, resUser.getData().get(0));
                            sendBroadcast(upIntent);
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
//                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }

    private void getUserData(int userid) {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_USER_ID, String.valueOf(userid));
        StringRequest req = new StringRequest(getString(R.string.BASE_URL) + getString(R.string.GetUserData))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        YNGetUserResponse resUser = gson.fromJson(s, YNGetUserResponse.class);
                        if (resUser != null && ReqUtils.isSuccess(resUser.getCode()) && resUser.getData().size() > 0) {
                            // 通知动态更新
                            Intent upIntent = new Intent(ACTIONs.actionUserData);
                            upIntent.putExtra(ACTIONs.UserGetData, resUser.getData().get(0));
                            sendBroadcast(upIntent);
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
//                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }

    // 获取成员动态
    private void getUserStatus(int userid, int loadId, int groupid, boolean isme) {
        String urlAddress;
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        if (isme) {
            urlAddress = getString(R.string.ynShowMeInfo);
        } else {
            mParam.put(CommonConstants.YNS_GROUP_ID, String.valueOf(groupid));
            urlAddress = getString(R.string.ynShowMemberInfo);
        }
        mParam.put(CommonConstants.YNS_USER_ID, String.valueOf(userid));
        mParam.put(CommonConstants.YNS_MIN_ID, String.valueOf(loadId));
        StringRequest req = new StringRequest(getString(R.string.BASE_URL) + urlAddress)
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
//                        Log.i("xjs","userssss===>"+s);
                        YNStatusResponse resUserStatus = gson.fromJson(s, YNStatusResponse.class);
                        if (resUserStatus != null && ReqUtils.isSuccess(resUserStatus.getCode())) {
                            // 清空状态列表并更新
                            List<YNStatus> statusLists = new ArrayList<>();
                            statusLists.addAll(resUserStatus.getData());
                            YNSingleton.getInstance().clearStatusList();
                            YNSingleton.getInstance().setStatusList(statusLists);
                            Intent upIntent = new Intent(ACTIONs.actionUserStatusUpdate);
                            sendBroadcast(upIntent);
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }



    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
